package org.zjx.app.strategy.impl;

import org.zjx.app.service.*;
import org.zjx.command.state.AccountState;
import org.zjx.command.state.GameState;
import org.zjx.command.state.QuestState;
import org.zjx.window.action.IAction;
import org.zjx.window.action.impl.DelayAction;
import org.zjx.window.strategy.IStrategy;
import org.zjx.window.window.IWindow;
import org.zjx.window.window.impl.GameWindow;
import org.zjx.window.window.impl.LoginWindow;
import org.zjx.window.window.impl.OpenWindow;

public class InitStrategy implements IStrategy {
    @Override
    public IAction analyze(IWindow window) {
        AccountState accountState = window.getState().getState(AccountState.class);
        if (accountState == null) {
            return new AccountServiceAction(window);
        }

        if (window instanceof GameWindow) {
            GameState gameState = window.getState().getState(GameState.class);
            if (!gameState.isGatherProp()) {
                return new PropServiceAction(window);
            } else if (!gameState.isGatherRole()) {
                return new RoleServiceAction(window);
            } else if (!gameState.isGatherQuest()) {
                return new QuestServerAction(window);
            } else if (!gameState.isGatherPet()){
                return new PetServiceAction(window);
            }
        }

        return new DelayAction(window, 60000);
    }

    @Override
    public String getName() {
        return "初始化";
    }

    @Override
    public int getPriority() {
        return 5;
    }

    @Override
    public boolean isApplicable(IWindow window) {
        GameState gameState = window.getState().getGameState();
        AccountState accountState = window.getState().getState(AccountState.class);
        if (window instanceof OpenWindow && accountState == null) {
            return true;
        } else if (window instanceof LoginWindow && accountState == null) {
            return true;
        } else if (window instanceof GameWindow) {
            if (accountState == null) {
                return true;
            } else if (!gameState.isGatherProp()) {
                return true;
            } else if (!gameState.isGatherQuest()) {
                return true;
            } else if (!gameState.isGatherPet()) {
                return true;
            } else return !gameState.isGatherRole();
        }
        return false;
    }
}
